#include "friendrequestwidget.h"
#include "ui_friendrequestwidget.h"
#include <QScrollArea>
#include "Core/tcpclient.h"
FriendRequestWidget::FriendRequestWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FriendRequestWidget)
{
    ui->setupUi(this);
    InitForUi();
    LinkUiToUiSignalAndSlot();
    LinkTcpSignalAndSlot();

}

FriendRequestWidget::~FriendRequestWidget()
{
    delete ui;
}

void FriendRequestWidget::SetUserName(QString Un)
{
    UserName=Un;
    userInfoLabel->setText(Un);

}

void FriendRequestWidget::InitForUi()
{
    setWindowTitle("申请加好友");

    // 创建用户信息标签
    userInfoLabel = new QLabel;
    userInfoLabel->setStyleSheet("font-size: 16px;");

        // 创建验证信息输入框
    QLabel *verifyMsgLabel = new QLabel("填写验证信息");
    verifyMsgEdit = new QLineEdit;
    verifyMsgEdit->setPlaceholderText("请输入验证信息");
    verifyMsgEdit->setText("我是谪仙");

    // 创建备注输入框
    QLabel *remarkLabel = new QLabel("备注");
    remarkEdit = new QLineEdit;

    // 创建分组下拉框
    QLabel *groupLabel = new QLabel("分组");
    groupComboBox = new QComboBox;
    groupComboBox->addItem("朋友");
    groupComboBox->addItem("家人");
    groupComboBox->addItem("同事");
    groupComboBox->addItem("其他");
    groupComboBox->setCurrentText("朋友");

    // 创建好友权限下拉框
    QLabel *permissionLabel = new QLabel("好友权限");
    permissionComboBox = new QComboBox;
    permissionComboBox->addItem("聊天、空间动态等");
    permissionComboBox->addItem("仅聊天");
    permissionComboBox->addItem("自定义权限");
    permissionComboBox->setCurrentText("聊天、空间动态等");

    // 创建发送和取消按钮
    sendBtn = new QPushButton("发送");
    cancelBtn = new QPushButton("取消");


    // 创建内容 widget
    QWidget *contentWidget = new QWidget;
    QVBoxLayout *contentLayout = new QVBoxLayout(contentWidget);
    contentLayout->addWidget(userInfoLabel);
    contentLayout->addWidget(verifyMsgLabel);
    contentLayout->addWidget(verifyMsgEdit);
    contentLayout->addWidget(remarkLabel);
    contentLayout->addWidget(remarkEdit);
    contentLayout->addWidget(groupLabel);
    contentLayout->addWidget(groupComboBox);
    contentLayout->addWidget(permissionLabel);
    contentLayout->addWidget(permissionComboBox);

    QHBoxLayout *btnLayout = new QHBoxLayout;
    btnLayout->addStretch();
    btnLayout->addWidget(sendBtn);
    btnLayout->addWidget(cancelBtn);
    contentLayout->addLayout(btnLayout);

    // 创建滚动区域
    QScrollArea *scrollArea = new QScrollArea(this);
    scrollArea->setWidgetResizable(true);
    scrollArea->setWidget(contentWidget);

     // 主布局
     QVBoxLayout *mainLayout = new QVBoxLayout(this);
     mainLayout->addWidget(scrollArea);

     // 设置窗口大小
     resize(400, 300);
}

void FriendRequestWidget::LinkUiToUiSignalAndSlot()
{
    connect(sendBtn,&QPushButton::clicked,
            this,&FriendRequestWidget::SendFriendRequest);
}

void FriendRequestWidget::LinkTcpSignalAndSlot()
{
    connect(this,&FriendRequestWidget::Sig_AddFriendRequest,
            TcpClient::get_instance(),&TcpClient::SendMsg);
}

void FriendRequestWidget::SendFriendRequest()
{

    QJsonObject Info;
    Info["TAG"]=UserName;
    Info["FROM"]=TcpClient::get_instance()->getUserName();
    Info["STATUS"]=FRIEND_REQUEST_STATUS_WAIT_DEAL;
    Info["SAY"]="I want to add friend with you";
    emit Sig_AddFriendRequest(MSG_TYPE_ADD_FRIEND_REQUEST,Info);
}
